<?php
declare(strict_types=1);

namespace Modules\CustomReport\Http\Controllers;

use Catch\Base\CatchController as Controller;
use Illuminate\Http\Request;use Modules\Base\Repository\MaterialRepository;
use Modules\CustomReport\Models\CustomReport;
use Modules\CustomReport\Http\Requests\ManageRequest;
use Modules\CustomReport\Repository\ManageRepository;

/**
 * 自定义报表控制器
 * Class CustomReportController
 * @package Modules\CustomReport\Http\Controllers
 */
class ManageController extends Controller
{
    public function __construct(
        protected readonly CustomReport $model
    ){}

    /**
     * @param Request $request
     * @return mixed
     */
    public function index(): mixed
    {
        $list = $this->model->getList()->toArray();
        foreach ($list['data'] as &$val){
            $val['route_path'] = "list/{$val['id']}";
        }
        return $list;
    }

    /**
     * @param Request $request
     * @return mixed
     */
    public function store(ManageRequest $request)
    {
        return ManageRepository::storeBy($request->post());
    }

    /**
     * @param $id
     * @return mixed
     */
    public function show($id)
    {
        $detail = $this->model->firstBy($id);
        if($detail){
            $detail = $detail->toArray();
            $detail['fields_json'] = json_decode($detail['fields_json'],true);
        }

        return $detail;
    }

    /**
     * @param Request $request
     * @param $id
     * @return mixed
     */
    public function update($id, ManageRequest $request)
    {
        return ManageRepository::storeBy($request->all(),$id);
    }

    /**
     * @param $id
     * @return mixed
     */
    public function destroy($id)
    {
        return $this->model->deleteBy($id);
    }

    /**
     * 显示表视图字段明细
     * @param $table
     */
    public function showTable($table){
        return ManageRepository::showTableDesc($table);
    }

    /**
     * 自定义报表配置信息获取
     * @param $id
     * @return mixed
     * @throws Exception
     */
    public function getConfigBy($id){
        return ManageRepository::getConfigById($id);
    }

    /**
     * 自定义报表数据分页列表
     * @param $id
     * @param Request $request
     */
    public function getList($id,Request $request){
        return ManageRepository::getList($id,$request->all());
    }
}
